package com.gongpingjialoan.data;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;

import com.gongpingjialoan.global.GPJApplication;
import com.gongpingjialoan.utility.PreferenceUtils;

public class UserFavSub {
	private ArrayList<String> favList = new ArrayList<String>();
	private ArrayList<String> subList = new ArrayList<String>();
	private String favString = new String();
	private String subString = new String();
	private static PreferenceUtils mPreferenceUtils = new PreferenceUtils(GPJApplication.getInstance());

	public UserFavSub() {
	}

	public void addFav(String favItem) {
		if (null != favList && null != favItem) {
			favList.add(favItem);
		}
	}

	public void clearFav() {
		if (null != favList) {
			favList.clear();
		}
	}

	public ArrayList<String> getFav() {
		if (0 != favString.length()) {
			String favtmp = this.favString.substring(1, this.favString.length() - 1);
			String[] favArray = favtmp.split(", ");
			for (int i = 0; i < favArray.length; ++i) {
				this.favList.add(favArray[i]);
			}
		}
		return favList;
	}

	public void addSub(String SubItem) {
		if (null != SubItem && null != subList) {
			subList.add(SubItem);
		}
	}

	public void clearSub() {
		if (null != subList) {
			subList.clear();
		}
	}

	public ArrayList<String> getSub() {
		if (0 != subString.length()) {
			String subtmp = this.subString.substring(1, this.subString.length() - 1);
			String[] subArray = subtmp.split(", ");
			for (int i = 0; i < subArray.length; ++i) {
				this.subList.add(subArray[i]);
			}
		}
		return subList;
	}

	public static ArrayList<String> loadFav() {
		UserFavSub loadOne = loadThis();
		if (null == loadOne) return null;

		return loadOne.getFav();
	}

	public static ArrayList<String> loadSub() {
		UserFavSub loadOne = loadThis();
		if (null == loadOne) return null;
		return loadOne.getSub();
	}

	private static UserFavSub loadThis() {
		return mPreferenceUtils.loadObject(UserFavSub.class);
	}

	public static void saveFav(JSONArray jsonArray) {
		if (null == jsonArray) return;
		UserFavSub tmp = new UserFavSub();
		ArrayList<String> tmpsub = UserFavSub.loadSub();
		if (tmp != null) {
			tmp.subList = tmpsub;
			tmp.subString = tmp.subList.toString();
		}

		for (int i = 0; i < jsonArray.length(); ++i) {
			try {
				tmp.addFav(String.valueOf(jsonArray.getJSONObject(i).getInt("car_id")));
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		tmp.favString = tmp.favList.toString();
		mPreferenceUtils.saveObject(tmp);
	}

	public static void saveSub(JSONArray jsonArray) {
		if (null == jsonArray) return;
		UserFavSub tmp = new UserFavSub();
		ArrayList<String> tmpfav = UserFavSub.loadFav();
		if (null != tmpfav) {
			tmp.favList = tmpfav;
			tmp.favString = tmpfav.toString();
		}
		for (int i = 0; i < jsonArray.length(); ++i) {
			try {
				tmp.addSub(String.valueOf(jsonArray.getJSONObject(i).getInt("sub_id")));
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		tmp.subString = tmp.subList.toString();
		mPreferenceUtils.saveObject(tmp);
	}
}
